﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public partial class frmDrugs : BaseWinFrom
{
    private Model.Drugs Drugs = null;

    public frmDrugs()
    {
        InitializeComponent();
    }

    public frmDrugs(Model.Drugs drugs)
        : this()
    {
        Drugs = drugs;
        if (drugs != null)
        {
            txtDrugsName.Text = drugs.Name;
            txtDrugsOrder.Text = drugs.Order.ToString();
            txtDrugsPrice.Text = drugs.Price.ToString();
            txtDrugsRate.Text = drugs.Race.ToString();
            ckbStatus.Checked = drugs.Status > 0;
        }
    }

    public frmDrugs(string DrugsId)
        : this(new RST.Drugs().Single(new Model.Drugs { Id = DrugsId }))
    {
    }

    private void btSave_Click(object sender, EventArgs e)
    {
        int Order;
        float Rate;
        float Price;
        int Status;
        
        if (!int.TryParse(txtDrugsOrder.Text, out Order))
        {
            Order = 0;
        }
        if (!float.TryParse(txtDrugsRate.Text, out Rate))
        {
            Rate = 0;
        }
        if (!float.TryParse(txtDrugsPrice.Text, out Price))
        {
            Price = 0;
        }
        if (ckbStatus.Checked)
        {
            Status = 1;
        }
        else
        {
            Status = 0;
        }

        if (Drugs == null)
        {
            Drugs = new Model.Drugs()
            {
                Name = txtDrugsName.Text,
                Order = Order,
                Race = Rate,
                Price = Price,
                Status = 1,
            };
            new RST.Drugs().Insert(Drugs);
        }
        else
        {
            Drugs.Name = txtDrugsName.Text;
            Drugs.Order = Order;
            Drugs.Race = Rate;
            Drugs.Price = Price;
            Drugs.Status = Status;
            new RST.Drugs().Update(Drugs);
        }
        this.Close();
    }
}